/*
 * PanelAlmacen.java
 *
 * Created on 26/01/2013, 09:43:06 AM
 */
package elaprendiz.ventanas.paneles;

import elaprendiz.controllers.CAlmacen;
import elaprendiz.modelbd.Almacen;
import elaprendiz.modelgui.ListModelAlmacen;
import elaprendiz.util.ECampos;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Ferz
 */
public class PanelAlmacen extends javax.swing.JPanel {

    /** Creates new form PanelAlmacen */
    private CAlmacen calmacen;
    private boolean esActualizacion = false;
    public PanelAlmacen() {
        initComponents();
        lstAlmacen.setModel(new ListModelAlmacen(tbntTodos.isSelected()));
        calmacen = new CAlmacen();
        lstAlmacen.setCellRenderer(new ListCellRenderer(){
            protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel renderer = (JLabel)defaultRenderer.getListCellRendererComponent(
                                    list, value, index, isSelected, cellHasFocus);
                Almacen mnd = (Almacen)value;
                if(mnd.getActivoBool())
                {
                   renderer.setIcon(new ImageIcon(this.getClass().getResource("/elaprendiz/resources/tick.png"))); 
                }else{
                    renderer.setIcon(new ImageIcon(this.getClass().getResource("/elaprendiz/resources/cancel.png"))); 
                }
                return renderer;
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstAlmacen = new javax.swing.JList();
        tbntTodos = new javax.swing.JToggleButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNombre = new elaprendiz.gui.textField.TextField();
        txtDireccion = new elaprendiz.gui.textField.TextField();
        cbEncargado = new elaprendiz.gui.comboBox.ComboBoxRectIcon();
        tbntActivado = new javax.swing.JToggleButton();
        pnlBotones = new javax.swing.JPanel();
        bntNuevo = new elaprendiz.gui.button.ButtonRect();
        bntModificar = new elaprendiz.gui.button.ButtonRect();
        bntGuardar = new elaprendiz.gui.button.ButtonRect();
        bntCancelar = new elaprendiz.gui.button.ButtonRect();
        bntSalir = new elaprendiz.gui.button.ButtonRect();

        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 130));

        lstAlmacen.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstAlmacen.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstAlmacenValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstAlmacen);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        tbntTodos.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tbntTodos.setSelected(true);
        tbntTodos.setText("Mostrando Activos");
        tbntTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbntTodosActionPerformed(evt);
            }
        });
        jPanel1.add(tbntTodos, java.awt.BorderLayout.PAGE_END);

        panelImage1.add(jPanel1, java.awt.BorderLayout.LINE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setOpaque(false);
        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Nombre:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel3.add(jLabel1, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Direccion:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel3.add(jLabel2, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Encargado:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        jPanel3.add(jLabel3, gridBagConstraints);

        txtNombre.setEditable(false);
        txtNombre.setPreferredSize(new java.awt.Dimension(200, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 0, 0);
        jPanel3.add(txtNombre, gridBagConstraints);

        txtDireccion.setEditable(false);
        txtDireccion.setPreferredSize(new java.awt.Dimension(300, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 0, 0);
        jPanel3.add(txtDireccion, gridBagConstraints);

        cbEncargado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3" }));
        cbEncargado.setEnabled(false);
        cbEncargado.setIcono(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/user.png"))); // NOI18N
        cbEncargado.setPreferredSize(new java.awt.Dimension(250, 26));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 0);
        jPanel3.add(cbEncargado, gridBagConstraints);

        tbntActivado.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tbntActivado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/cancel.png"))); // NOI18N
        tbntActivado.setSelected(true);
        tbntActivado.setText("Activado");
        tbntActivado.setEnabled(false);
        tbntActivado.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/tick.png"))); // NOI18N
        tbntActivado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbntActivadoActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        jPanel3.add(tbntActivado, gridBagConstraints);

        jPanel2.add(jPanel3);

        pnlBotones.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlBotones.setOpaque(false);

        bntNuevo.setBackground(new java.awt.Color(51, 153, 255));
        bntNuevo.setText("Nuevo");
        bntNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntNuevoActionPerformed(evt);
            }
        });
        pnlBotones.add(bntNuevo);

        bntModificar.setBackground(new java.awt.Color(51, 153, 255));
        bntModificar.setText("Modificar");
        bntModificar.setEnabled(false);
        bntModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntModificarActionPerformed(evt);
            }
        });
        pnlBotones.add(bntModificar);

        bntGuardar.setBackground(new java.awt.Color(51, 153, 255));
        bntGuardar.setText("Guardar");
        bntGuardar.setEnabled(false);
        bntGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGuardarActionPerformed(evt);
            }
        });
        pnlBotones.add(bntGuardar);

        bntCancelar.setBackground(new java.awt.Color(51, 153, 255));
        bntCancelar.setText("Cancelar");
        bntCancelar.setEnabled(false);
        bntCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarActionPerformed(evt);
            }
        });
        pnlBotones.add(bntCancelar);

        bntSalir.setBackground(new java.awt.Color(51, 153, 255));
        bntSalir.setText("Salir");
        pnlBotones.add(bntSalir);

        jPanel2.add(pnlBotones);

        panelImage1.add(jPanel2, java.awt.BorderLayout.CENTER);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void bntNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntNuevoActionPerformed
    txtDireccion.setEditable(true);
    txtNombre.setEditable(true);
    txtDireccion.setText("");
    txtNombre.setText("");
    cbEncargado.setSelectedIndex(0);
    cbEncargado.setEnabled(true);
    ECampos.buscarBotones(pnlBotones, false, new Component[]{bntSalir,bntCancelar});
    bntGuardar.setEnabled(true);
    bntCancelar.setEnabled(true);
}//GEN-LAST:event_bntNuevoActionPerformed

private void bntCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarActionPerformed
    txtDireccion.setEditable(false);
    txtNombre.setEditable(false);
    txtDireccion.setText("");
    txtNombre.setText("");
    cbEncargado.setSelectedIndex(0);
    cbEncargado.setEnabled(false);
    ECampos.buscarBotones(pnlBotones, true, new Component[]{bntSalir,bntCancelar});
    bntGuardar.setEnabled(false);
    bntCancelar.setEnabled(false);
}//GEN-LAST:event_bntCancelarActionPerformed

private void bntModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntModificarActionPerformed
    txtDireccion.setEditable(true);
    txtNombre.setEditable(true);
    cbEncargado.setEnabled(true);
    ECampos.buscarBotones(pnlBotones, false, new Component[]{bntSalir,bntCancelar});
    bntGuardar.setEnabled(true);
    bntCancelar.setEnabled(true);
    esActualizacion = true;
}//GEN-LAST:event_bntModificarActionPerformed

private void bntGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntGuardarActionPerformed
  if(!esActualizacion)
  {
      guardar();
  }else
  {
      actualizar();
  }
}//GEN-LAST:event_bntGuardarActionPerformed

private void lstAlmacenValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstAlmacenValueChanged
    if(lstAlmacen.getSelectedValue() != null)
    {
        Almacen tm = (Almacen)lstAlmacen.getSelectedValue();
        txtNombre.setText(tm.getNombre());
        txtDireccion.setText(tm.getDireccion());
        cbEncargado.setSelectedItem(tm.getEncargado().toString());
        tbntActivado.setEnabled(true);
        bntModificar.setEnabled(true);
        tbntActivado.setSelected(tm.getActivoBool());
    }
}//GEN-LAST:event_lstAlmacenValueChanged

private void tbntActivadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbntActivadoActionPerformed
 if(tbntActivado.isSelected())
 {
     tbntActivado.setText("Activo");
 }else
 {
     tbntActivado.setText("No Activo");
 }
 
 activar(tbntActivado.isSelected());
}//GEN-LAST:event_tbntActivadoActionPerformed

private void tbntTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbntTodosActionPerformed
if(tbntTodos.isSelected())
 {
     tbntTodos.setText("Mostrando Activos");
 }else
 {
     tbntTodos.setText("Mostrando Todos");
 }
lstAlmacen.setModel(new ListModelAlmacen(tbntTodos.isSelected())); 
}//GEN-LAST:event_tbntTodosActionPerformed


public void guardar()
{
      if(!txtNombre.getText().isEmpty() && !this.txtDireccion.getText().isEmpty() &&
            this.cbEncargado.getSelectedIndex() > 0)
        {
            Almacen na = new Almacen();
            na.setNombre(txtNombre.getText());
            na.setDireccion(txtDireccion.getText());
            na.setEncargado(Integer.parseInt(cbEncargado.getSelectedItem().toString()));
            boolean guardarRegistro = calmacen.guardarRegistro(na);
            if(guardarRegistro)
            {
                JOptionPane.showMessageDialog(null, "Almacen Agregado");  
                asignarEncargado(calmacen.getUltimoAlmacen());
                lstAlmacen.setModel(new ListModelAlmacen(tbntTodos.isSelected()));
            }
        }
}

private void asignarEncargado(Almacen alm)
{
    EncargadoAlmacenPanel pvc = new EncargadoAlmacenPanel(alm);
       JLabel aviso = new JLabel();
       aviso.setVisible(false);
       
        JOptionPane.showInternalOptionDialog(this, pvc, "Asignar Encargados.",JOptionPane.OK_CANCEL_OPTION,
                                            JOptionPane.QUESTION_MESSAGE, null, new Object[]{aviso},null);
   
        if(pvc.EsAceptado())
        {
           pvc.guardar();          
        } 
}
public void actualizar()
{
    if(lstAlmacen.getSelectedValue() != null)
    {
        Almacen na = (Almacen)lstAlmacen.getSelectedValue();
        if(!txtNombre.getText().isEmpty() && !this.txtDireccion.getText().isEmpty() &&
            this.cbEncargado.getSelectedIndex() > 0)
        {
            if(!txtNombre.getText().equals(na.getNombre()) || 
                   txtDireccion.getText().equals(na.getDireccion()) ||
                    cbEncargado.getSelectedItem().toString().equals(na.getEncargado().toString()))
            na.setNombre(txtNombre.getText());
            na.setDireccion(txtDireccion.getText());
            na.setEncargado(Integer.parseInt(cbEncargado.getSelectedItem().toString()));
            int  guardarRegistro = calmacen.actualizarRegistro(na);
            if(guardarRegistro != 0)
            {
                JOptionPane.showMessageDialog(null, "Almacen Actualizado");
                lstAlmacen.setModel(new ListModelAlmacen(tbntTodos.isSelected()));
                esActualizacion = false;
            }
        }
    }
}

public void activar(boolean opcion)
{
    if(lstAlmacen.getSelectedValue() != null)
    {
        Almacen na = (Almacen)lstAlmacen.getSelectedValue();
        
            na.setActivo(opcion);
            int  guardarRegistro = calmacen.actualizarEstado(na);
            if(guardarRegistro != 0)
            {
                JOptionPane.showMessageDialog(null, (opcion?"Almacen Activado":"Almacen desactivado"));
                lstAlmacen.setModel(new ListModelAlmacen(tbntTodos.isSelected()));
                esActualizacion = false;
            }
        
    }
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect bntCancelar;
    private elaprendiz.gui.button.ButtonRect bntGuardar;
    private elaprendiz.gui.button.ButtonRect bntModificar;
    private elaprendiz.gui.button.ButtonRect bntNuevo;
    private elaprendiz.gui.button.ButtonRect bntSalir;
    private elaprendiz.gui.comboBox.ComboBoxRectIcon cbEncargado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstAlmacen;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JToggleButton tbntActivado;
    private javax.swing.JToggleButton tbntTodos;
    private elaprendiz.gui.textField.TextField txtDireccion;
    private elaprendiz.gui.textField.TextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
